package com.guoning.office_system.service.impl;

import com.guoning.office_system.mapper.EquipmentMapper;
import com.guoning.office_system.model.Equipment;
import com.guoning.office_system.model.EquipmentWithMeetingRoom;
import com.guoning.office_system.model.MeetingRoom;
import com.guoning.office_system.service.EquipmentService;
import com.guoning.office_system.service.MeetingRoomService;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    EquipmentMapper equipmentMapper;
    @Autowired
    MeetingRoomService meetingRoomService;

    @Override
    public Long selectByEquipmentCodeCount(String equipmentCode) {
        return equipmentMapper.selectByEquipmentCodeCount(equipmentCode);
    }

    @Override
    public PageBean selectByCondition(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", pageBean.getPageIndex() * pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        List<EquipmentWithMeetingRoom> list = equipmentMapper.selectByCondition(criteria);
        Long count = equipmentMapper.selectByConditionCount(criteria);
        for (int i=0; i<list.size(); i++){
            MeetingRoom meetingRoom = meetingRoomService.selectByPrimaryKey(list.get(i).getRoomId());
            list.get(i).setMeetingRoom(meetingRoom);
        }
        pageBean.setResultList(list);
        pageBean.setTotalCount(count);
        return pageBean;
    }

    @Override
    public int updateEquipmentStatus(List list) {
        return equipmentMapper.updateEquipmentStatus(list);
    }

    @Override
    public int updateForDelete(List list) {
        return equipmentMapper.updateForDelete(list);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public Long insert(Equipment record) {
        record.setEquipmentStatus(1);
        equipmentMapper.insert(record);
        return record.getId();
    }

    @Override
    public int insertSelective(Equipment record) {
        return 0;
    }

    @Override
    public Equipment selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(Equipment record) {
        return equipmentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Equipment record) {
        return 0;
    }
}